﻿using System;
using MyLib;

namespace WebRole1
{
    public partial class ExplorerTable : BasicPage
    {

        protected string MyInfo = "";
        protected string MyList = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!ctx.MyClsStorageAccount.IsAccountValid)
            {
                Response.Redirect("login.aspx");
            }

            if (!IsPostBack)
            {
                mInit();
            }
        }

        private void mInit()
        {
            try
            {
                RefreshListOfTables();
            }
            catch (Exception ex)
            {
                TraceException(ex);
            }

        }

        protected void btnRefreshListTables_Click(object sender, EventArgs e)
        {
            try
            {
                listTables.DataSource = MyLib.MyAPI.TableList;
                listTables.DataBind();
            }
            catch (Exception ex)
            {
                TraceException(ex);
            }
        }

        private void RefreshListOfTables()
        {
            listTables.DataSource = MyLib.MyAPI.TableList;
            listTables.DataBind();
        }


        private void TraceException(Exception ex)
        {
            MyInfo = MyLib.MyAPI.GetErrorMessage(ex);
        }


        protected void btnGetRecords_Click(object sender, EventArgs e)
        {
            try
            {
                GetRecords();
            }
            catch (Exception ex)
            {
                TraceException(ex);
            }
        }

        private void GetRecords()
        {
            int iAmount = 0;
            if (!int.TryParse(txtAmountOfRecords.Text, out iAmount))
            {
                iAmount = 200;
            }
            if ((iAmount < 1) || iAmount > 2000)
            {
                iAmount = 200;
            }
            txtAmountOfRecords.Text = iAmount.ToString();

            string tableName = listTables.SelectedValue;
            var list = MyLib.MyAPI.Records(tableName, iAmount);
            listRecords.DataSource = list;
            listRecords.DataBind();

            lblRecords.Text = "Records" + "(" + list.Count.ToString() + ")";

        }

        protected void btnDeleteAllRecords_Click(object sender, EventArgs e)
        {
            try
            {
                DeleteAllRecords();
            }
            catch (Exception ex)
            {
                TraceException(ex);
            }

        }

        private void DeleteAllRecords()
        {
            string tableName = listTables.SelectedValue;
            if (tableName == "")
            {
                return;
            }
            int i = MyLib.MyAPI.DeleteAllRecordsFromTable(tableName);

            MyInfo = "Amount of deleted record is " + i.ToString();

            GetRecords();
        }
    }
}